<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>表单模块 - layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="apple-mobile-web-app-status-bar-style" content="black"> 
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="format-detection" content="telephone=no">
  <link rel="stylesheet" href="./css/layui.css">
    <script src="js/adddevices.js"></script>
<style>
body{padding: 10px;}
</style>
</head>
<body>

<form class="layui-form layui-form-pane1" action="" lay-filter="first">
  <div class="layui-form-item">
    <label class="layui-form-label">主机名</label>
    <div class="layui-input-block">
      <input type="text" name="hostname" lay-verify="required|title" lay-reqText="主机名不能为空" required placeholder="请输入主机名称" autocomplete="off" class="layui-input" >
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">IP地扯</label>
    <div class="layui-input-block">
      <input type="text" name="ipaddr" lay-verify="required|title" lay-reqText="IP地扯不能为空" required placeholder="请输入IP地扯" autocomplete="off" class="layui-input" >
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">端口</label>
    <div class="layui-input-block">
      <input type="text" name="port" lay-verify="required|title" lay-reqText="端口不能为空" required placeholder="请输入主机端口" autocomplete="off" class="layui-input" >
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">设备组</label>
    <div class="layui-input-block">
      <select id="Groups" name="Groups" lay-filter="interest">
      </select>
    </div>
  </div>

    <div class="layui-form-item">
    <label class="layui-form-label">用户名</label>
    <div class="layui-input-block">
      <select id="username" name="username" lay-filter="interest">
      </select>
    </div>
  </div>

      <div class="layui-form-item">
    <label class="layui-form-label">密码</label>
    <div class="layui-input-block">
      <select id="pwd" name="pwd" lay-filter="interest">
      </select>
    </div>
  </div>

    <div class="layui-form-item" pane>
    <label class="layui-form-label">检测</label>
    <div class="layui-input-block">
      <input type="checkbox" checked name="check" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">
    </div>
  </div>

  <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">请填写描述</label>
    <div class="layui-input-block">
      <textarea class="layui-textarea" name="desc"></textarea>
    </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="*">添加</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>

<br><br><br>


<script src="./js/layui.js"></script>
<!-- <script src="../js/layui.all.js"></script> -->

<script>

layui.use(['form', 'layedit', 'laydate'], function(){
  const form = layui.form
          , layer = layui.layer
          , layedit = layui.layedit
          , laydate = layui.laydate;



  //自定义验证规则
  form.verify({
    title: function(value){
      if(value.length < 1){
        return '输入的太短了吧';
      }
    }
    ,pass: [/(.+){6,12}$/, '密码必须6到12位']
  });

  //初始赋值
  const thisValue = form.val('first', {
    'port': '22'
  });


  //事件监听
  form.on('input', function(data){
    console.log('input: ', this, data);
  });

  form.on('select', function(data){
   console.log('select: ', thisValue);
  });
  
  form.on('switch', function(data){
  });

  
  //监听提交
  form.on('submit(*)', function(data){
      alert("test");
      const data1=data.field
      const data2 = JSON.stringify(data1);
      const addvices = new XMLHttpRequest();
            addvices.open('post','http://localhost:5000/system');
            addvices.send(data2);
      addvices.onreadystatechange = function () {
          console.log(addvices.responseText);
          if (addvices.readyState == 4 && addvices.responseText == "1"){
              console.log(addvices.responseText)
              alert(addvices.responseText);
              alert("添加成功")
          } else {
              alert("添加失败")
              alert(addvices.responseText);
          }
      }
    return false;
  });
  
});

</script>


<script>
layui.use('jquery', function(){
  var $ = layui.jquery;
  var submit = function(){
    return false;
  };
  $('#test').on('submit', function(){
     return false
  });
  $('#test1').on('submit', function(obj){
    //console.log(obj.field)
     //return false;
  });
});
</script>
</body>
</html>
